import 'package:flutter/material.dart';
import 'package:x_custom_widget/custom_text.dart';

import 'log.dart';

class LogEntry extends StatefulWidget {
  final Widget body;
  const LogEntry({Key? key, required this.body}) : super(key: key);

  @override
  State createState() => _LogEntryState();
}

class _LogEntryState extends State<LogEntry>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _scaleAnimation;

  @override
  void initState() {
    // 初始化 AnimationController
    _controller = AnimationController(
      duration: const Duration(seconds: 1),
      vsync: this,
    );

    // 设置 Tween 的起始和结束值
    _scaleAnimation = Tween<double>(begin: 0.5, end: 1.0).animate(_controller)
      ..addListener(() {
        setState(() {}); // 触发状态更新以重新绘制 UI
      });
    _controller.forward();

    // // 开始动画
    // _controller.forward();
    super.initState();
  }

  Offset _offset = Offset.zero;

  @override
  Widget build(BuildContext context) {
    return Log.disable
        ? widget.body
        : LogProvider(
            themeColor: Colors.red,
            child: Scaffold(
                resizeToAvoidBottomInset: false,
                body: Stack(
                  children: [
                    widget.body,
                    StreamBuilder(
                      initialData: 0, // 设置初始数据
                      stream: logController.stream,
                      builder: (context, snapshot) {
                        return Positioned(
                          bottom: 100 - _offset.dy,
                          left: 10 + _offset.dx,
                          child: GestureDetector(
                              onPanUpdate: (details) {
                                setState(() {
                                  _offset += details.delta;
                                });
                              },
                              onPanEnd: (_) {
                                setState(() {
                                  // 可以在这里添加额外的逻辑，例如重置位置
                                });
                              },
                              onLongPress: () {},
                              onTap: () {
                                Navigator.pop(context);
                                // if (Get.currentRoute == '/log') {
                                //   Get.back();
                                // } else if (Get.previousRoute == '/log') {
                                //   Get.back();
                                // } else {
                                //   Get.toNamed('/log');
                                // }
                              },
                              child: AnimatedBuilder(
                                animation: _scaleAnimation,
                                builder: (context, child) {
                                  return Transform.scale(
                                    scale: _scaleAnimation.value,
                                    child: CustomText(
                                      snapshot.hasData
                                          ? snapshot.data.toString()
                                          : "NET",
                                      fontSize: 17,
                                      width: 35,
                                      height: 35,
                                      color: snapshot.data.toString() == "200"
                                          ? Colors.green
                                          : Colors.red,
                                      fontWeight: FontWeight.w500,
                                      bgColor: Colors.black,
                                      textAlign: TextAlign.center,
                                      crossAxisAlignment:
                                          CrossAxisAlignment.center,
                                      mainAxisAlignment:
                                          MainAxisAlignment.center,
                                      radius: 15,
                                    ),
                                  );
                                },
                              )),
                        );
                      },
                    )
                  ],
                )),
          );
  }
}

class LogProvider extends InheritedWidget {
  final Color themeColor;
  LogProvider({required Widget child, required this.themeColor})
      : super(child: child);

  static LogProvider? of(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType<LogProvider>();
  }

  @override
  bool updateShouldNotify(covariant InheritedWidget oldWidget) {
    return true;
  }
}
